<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use app\model\Adv as AdvModel;
use app\validate\User as UserValidate;
use app\middleware\Auth as AuthMiddleware;
class Adv
{
    protected $middleware = [AuthMiddleware::class];
    private $toast = 'public/toast';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        return view('index', [
            'list'  =>   AdvModel::withSearch(['id', 'text','img', 'url','position', 'is_img','create_at'], [
                'id'        =>  request()->param('id'),
                'text'      =>  request()->param('text'),
                'position'  =>  request()->param('position'),
                'create_at' =>  request()->param('create_at')
            ])->paginate([
                'query'         =>  request()->param()
            ])
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->param();
        //写入数据库
        $id = AdvModel::create($data)->getData('id');

        //返回
        return $id ? view($this->toast, [
            'infos'     =>  ['添加成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/adv')
        ]) : '添加失败';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        return view('edit', [
            'obj'   =>  AdvModel::find($id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        if (!$data['img']){
            $data['img'] = AdvModel::where('id',$data['id'])->column('img');
        }
        try {
            validate(UserValidate::class)
                ->scene('adv')
                ->batch(true)
                ->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/adv/'.$id.'/edit')
            ]);
        }

        return AdvModel::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/adv')
        ]) : '修改失败';
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return AdvModel::destroy($id) ? view($this->toast, [
            'infos'     =>  ['恭喜，删除成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/adv')
        ]) : '删除失败！';
    }
}
